package org.jeecg.modules.rental.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.rental.constant.RentalCacheConstant;
import org.jeecg.modules.rental.entity.RentalEBikeType;
import org.jeecg.modules.rental.mapper.RentalEBikeTypeMapper;
import org.jeecg.modules.rental.service.IRentalEBikeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 车辆类型
 * @Author: sean
 * @Date:   2024-04-26
 * @Version: V1.0
 */
@Service
public class RentalEBikeTypeServiceImpl extends ServiceImpl<RentalEBikeTypeMapper, RentalEBikeType> implements IRentalEBikeTypeService {

    @Autowired
    private CacheManager cacheManager;

    @Override
    public IPage<RentalEBikeType> selectJoinPage(Page page, RentalEBikeType rentalCarType) {
        return baseMapper.selectJoinPage(page, rentalCarType);
    }

    @Override
    public RentalEBikeType get(String id) {
        Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_EBIKE_TYPE_CACHE);
        String key = TenantContext.getTenant()+":"+id;
        RentalEBikeType type = cache.get(key, RentalEBikeType.class);
        if(oConvertUtils.isEmpty(type)){
            type = this.getById(id);
            if(oConvertUtils.isEmpty(type)){
                cache.put(key, type);
            }
        }
        return type;
    }

    @Override
    public RentalEBikeType add(RentalEBikeType entity) {
        if(!ObjectUtils.isEmpty(entity)){
            //保存数据库
            this.saveOrUpdate(entity);
            String key = TenantContext.getTenant() + ":" + entity.getId();
            Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_EBIKE_TYPE_CACHE);
            cache.put(key, entity); //将数据放入缓存
        }
        return entity;
    }

    @Override
    public RentalEBikeType edit(RentalEBikeType entity) {
        if(!ObjectUtils.isEmpty(entity)){
            //保存数据库
            this.saveOrUpdate(entity);
            String key = TenantContext.getTenant() + ":" + entity.getId();
            Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_EBIKE_TYPE_CACHE);
            cache.put(key, entity); //将数据放入缓存
        }
        return entity;
    }

    @Override
    public Boolean deleteById(String id) {
        Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_EBIKE_TYPE_CACHE);
        String key = TenantContext.getTenant() + ":" + id;
        cache.evict(key); //删除缓存
        return this.removeById(id);
    }

    @Override
    public Boolean deleteBatch(List<String> ids) {
        Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_EBIKE_TYPE_CACHE);
        if (cache != null) {
            ids.forEach(id->{
                String key = TenantContext.getTenant() + ":" + id;
                cache.evict(key); //删除缓存
            });
        }
        return this.removeByIds(ids);
    }

    @Override
    public void initCacheCarType() {
        List<RentalEBikeType> typeList = this.list();
        Cache cache = cacheManager.getCache(RentalCacheConstant.RENTAL_EBIKE_TYPE_CACHE);
        if (cache != null) {
            typeList.forEach(item->{
                String key = TenantContext.getTenant() + ":" + item.getId();
                cache.put(key, item); //删除缓存
            });
        }
    }

}
